#include<stdio.h>
#include<stdlib.h>

#define MAX_LEN 10
typedef int DataType;
typedef struct circlequeue
{
    DataType data[MAX_LEN];
    int rear;
    int front;
    int num;
}CircleQueue;

CircleQueue *creatCircleQueue()
{
    CircleQueue *cq=malloc(sizeof(CircleQueue));
    cq->front=-1;
    cq->rear=-1;
    cq->num=0;
    return cq;
}

int EnQuqu(CircleQueue *cq,int data)
{
    if(cq==NULL||cq->num==MAX_LEN)
    {
        return 0;
    }
    if(cq->num==0)
    {
        cq->front=(cq->front+1)%MAX_LEN;
        cq->rear=(cq->rear+1)%MAX_LEN;
        cq->data[cq->rear]=data;
    }
    else
    {
        cq->rear=(cq->rear+1)%MAX_LEN;
        cq->data[cq->rear]=data;
    }
    cq->num++;
    return 1;
}

int DeQuqu(CircleQueue *cq)
{
    if(cq==NULL||cq->num==0)
    {
        return 0;
    }
    DataType data=cq->data[cq->front];
    cq->front=(cq->front+1)%MAX_LEN;
    cq->num--;
    return data;
}

void DestroyQueue(CircleQueue *cq)
{
    if(cq==NULL)
    {
        return;
    }
    cq->front=-1;
    cq->rear=-1;
    cq->num=0;
    free(cq);
}

void ClearQueue(CircleQueue *cq)
{
    if(cq==NULL)
    {
        return;
    }
    cq->front=-1;
    cq->rear=-1;
    cq->num=0;
}

int QueueLen(CircleQueue *cq)
{
    if(cq==NULL||cq->num==0)
    {
        return 0;
    }
    return cq->num;
}

DataType GetHead(CircleQueue *cq)
{
    if(cq==NULL||cq->num==0)
    {
        return 0;
    }
    return cq->data[cq->front];
}

void PrintQueue(CircleQueue *cq)
{
    if(cq==NULL)
    {
        printf("Queue is Null!\n");
        return;
    }
    else if(cq->num==0)
    {
        printf("Queue is empty!\n");
        return;
    }
    for(int i=0;i<cq->num;i++)
    {
        printf("%d ",cq->data[(cq->front+i)%MAX_LEN]);
    }
    printf("\n");
}


int main()
{
    CircleQueue *cq=creatCircleQueue();
    EnQuqu(cq,1);
    EnQuqu(cq,2);
    EnQuqu(cq,3);
    EnQuqu(cq,4);
    EnQuqu(cq,5);
    EnQuqu(cq,6);
    EnQuqu(cq,7);
    EnQuqu(cq,8);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    PrintQueue(cq);
    EnQuqu(cq,9);
    EnQuqu(cq,10);
    EnQuqu(cq,11);
    EnQuqu(cq,12);
    PrintQueue(cq);printf("length:%d\n",QueueLen(cq));printf("head:%d\n",GetHead(cq));
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    DeQuqu(cq);
    PrintQueue(cq);
    
}